package com.transcendence.appwidget.updatetime;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.widget.RemoteViews;

import androidx.annotation.Nullable;


import com.transcendence.appwidget.R;
import com.transcendence.core.utils.date.DateUtils;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author joephone
 * @date 2023/9/14
 * @desc
 */
public class UpdateTimeService extends Service {
//    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Timer timer;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                update();
            }
        },0,1000);
    }

    private void update() {
        //更新的逻辑
        String time = DateUtils.getInstance().formatDate2String(System.currentTimeMillis()+""); // sdf.format(new Date());

        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_two_three);
        remoteViews.setTextViewText(R.id.tv_name_type,time);
        AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext());
        ComponentName componentName =new ComponentName(getApplicationContext(), UpdateTimeWidgetProvider.class);
        manager.updateAppWidget(componentName,remoteViews);

    }
}

